package com.example.android_app.database;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MyDataBase {
    private String UserTable = "User";
    private DataBaseHelper dataBaseHelper;

    public MyDataBase(Context context, String database_name, int version){
        dataBaseHelper = new DataBaseHelper(context, database_name, null, version);
    }

    // 需增加判断用户是否已经被注册的功能
    public Boolean query(String username, String password){
        String[] select = {"username"};
        String[] Args = {username, password};
        SQLiteDatabase sqLiteDatabase = dataBaseHelper.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.query(UserTable, select, "username=? and password=?", Args, null, null, null );
        if (cursor!=null) {
            if(cursor.getCount()==1){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }


    public void addData(String username, String password) {
        SQLiteDatabase sqLiteDatabase = dataBaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", username);
        values.put("password", password);

        sqLiteDatabase.insert(UserTable, null, values);

    }

    public Boolean query_username(String username){
        String[] select = {"username"};
        String[] Args = {username};
        SQLiteDatabase sqLiteDatabase = dataBaseHelper.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.query(UserTable, select, "username=?", Args, null, null, null );
        if (cursor!=null) {
            if(cursor.getCount()==1){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }
    public void updateData(String username, String password){
        SQLiteDatabase sqliteDatabase = dataBaseHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", username);
        values.put("password", password);
        sqliteDatabase.update("User", values, "username=?", new String[] { username });
    }
}
